package com.ruoyi.personnel.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.Personnel;
import com.ruoyi.personnel.service.IPersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人口Controller
 * 
 * @author xjp
 * @date 2024-06-29
 */
@RestController
@RequestMapping("/personnel/personnel")
public class PersonnelController extends BaseController
{
    @Autowired
    private IPersonnelService personnelService;

    /**
     * 查询人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('personnel:personnel:list')")
    @GetMapping("/list")
    public TableDataInfo list(Personnel personnel)
    {
        startPage();
        List<Personnel> list = personnelService.selectPersonnelList(personnel);
        return getDataTable(list);
    }

    /**
     * 导出人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('personnel:personnel:export')")
    @Log(title = "人口/人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Personnel personnel)
    {
        List<Personnel> list = personnelService.selectPersonnelList(personnel);
        ExcelUtil<Personnel> util = new ExcelUtil<Personnel>(Personnel.class);
        util.exportExcel(response, list, "人员信息数据");
    }

    /**
     * 获取人员信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:personnel:query')")
    @GetMapping(value = "/{pId}")
    public AjaxResult getInfo(@PathVariable("pId") Long pId)
    {
        return success(personnelService.selectPersonnelByPId(pId));
    }

    /**
     * 新增人员信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:personnel:add')")
    @Log(title = "人口/人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Personnel personnel)
    {
        return toAjax(personnelService.insertPersonnel(personnel));
    }

    /**
     * 修改人员信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:personnel:edit')")
    @Log(title = "人口/人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Personnel personnel)
    {
        return toAjax(personnelService.updatePersonnel(personnel));
    }

    /**
     * 删除人员信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:personnel:remove')")
    @Log(title = "人口/人员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pIds}")
    public AjaxResult remove(@PathVariable Long[] pIds)
    {
        return toAjax(personnelService.deletePersonnelByPIds(pIds));
    }

    @Log(title = "项目编号", businessType = BusinessType.IMPORT) // todo
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<Personnel> util = new ExcelUtil<>(Personnel.class); // todo
        List<Personnel> PersonnelList = util.importExcel(file.getInputStream()); // todo
        System.out.println(PersonnelList.toString());
        String operName = getUsername();
        String message = personnelService.importUser(PersonnelList, updateSupport, operName); // todo
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Personnel> util = new ExcelUtil<>(Personnel.class); // todo
        util.importTemplateExcel(response, "用户数据模板");
    }
    @Anonymous
    @PostMapping("/getUnitByIdCard")
    public AjaxResult getUnitByIdCard(@RequestBody String idCard)
    {

        return AjaxResult.success(personnelService.selectPersonnelByIDcard(idCard));
    }

    @PostMapping("/updateCostState")
    public AjaxResult updateCostState(@RequestBody String idCard)
    {
        return AjaxResult.success(personnelService.updatePersonnelByIdCard(idCard));
    }




}
